//
//  JCEventBinding.m
//  JCSDK
//
//  Created by master on 16/3/17.
//  Copyright © 2016年 AdMaster. All rights reserved.
//

#import "JCEventBinding.h"
#import "JCUIControlBinding.h"
#import "JCUITableViewBinding.h"
@implementation JCEventBinding
+ (id)bindingWithJSONObject:(id)object {
    if (object == nil) {
        NSLog(@"must supply an json object not null");
        return nil;
    }
    NSString *bindingType = object[@"event_type"];
    Class class = [self classFromString:bindingType];
    return [class bindingWithJSONObject:object];
}

+ (NSString *)typeName {
    
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
                                 userInfo:nil];
}
+ (Class)classFromString:(NSString *)bindingType {
    NSDictionary *classMap = @{
                               [JCUITableViewBinding typeName] : [JCUITableViewBinding class],
                               [JCUIControlBinding typeName]   : [JCUIControlBinding class]
                               };
    return classMap[bindingType];
}
- (void)execute
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
                                 userInfo:nil];
}

- (void)stop
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
                                 userInfo:nil];
}


- (instancetype)initWithEventName:(NSString *)eventName onPath:(NSString *)path
{
    if (self = [super init]) {
        self.eventName = eventName;
        self.path = [[JCObjectSelector alloc] initWithString:path];
        
        self.path = [[JCObjectSelector alloc] initWithString:path];
        self.name = [[NSUUID UUID] UUIDString];
        self.running = NO;
    }
    return self;
}

#pragma mark -- NSCoder

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    NSString *path = [aDecoder decodeObjectForKey:@"path"];
    NSString *eventName = [aDecoder decodeObjectForKey:@"eventName"];
    if (self = [self initWithEventName:eventName onPath:path]) {
        self.ID = [[aDecoder decodeObjectForKey:@"ID"] unsignedLongValue];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.swizzleClass = NSClassFromString([aDecoder decodeObjectForKey:@"swizzleClass"]);
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:@(_ID) forKey:@"ID"];
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeObject:_path.string forKey:@"path"];
    [aCoder encodeObject:_eventName forKey:@"eventName"];
    [aCoder encodeObject:NSStringFromClass(_swizzleClass) forKey:@"swizzleClass"];
}


+ (void)track:(NSString *)event properties:(NSDictionary *)properties
{
    NSMutableDictionary *bindingProperties = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @YES, @"$from_binding", nil];
    [bindingProperties addEntriesFromDictionary:properties];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"notice" message:@"点击检测" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
//    [[Mixpanel sharedInstance] track:event properties:bindingProperties];
}

@end
